package cn.ys.groupbuy;

import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import cn.ys.others.MyDataBaseHelper;
import cn.ys.others.MyProgressDialog;

//import android.app.FragmentManager;


public class GroupBuyActivity extends Activity implements View.OnClickListener {

    private TuanFragment tuanFragment;
    private ShangMenFragment shangMenFragment;
    private BusinessFragment businessFragment;
    private MineFragment mineFragment;
    private MoreFragment moreFragment;
    private TextView cityTextView;
    private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
    private Dialog dialog=null;
    private LocationClient mLocationClient;
    private ImageView menu_deal_image,menu_apollo_image,menu_poi_image,menu_user_image,menu_more_image;
    private TextView menu_deal_text,menu_apollo_text,menu_poi_text,menu_user_text,menu_more_text;

    private ImageButton openMap;
    private boolean isStopThread=false;
    private FrameLayout content_fragment;
    private ImageView loading_img1,loading_img2;
    private int timeCount=0;
    private int fragmentNum=2;
    private MyDataBaseHelper dataBaseHelper;
    private SQLiteDatabase db;
    private LocationApplication application;
    //private int image[]={R.mipmap.refreshing_image_01,R.mipmap.refreshing_image_02};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_buy);
        application= (LocationApplication) getApplication();
        cityTextView=(TextView)findViewById(R.id.city);


        ContentValues values=new ContentValues();
       // values.put("user","bala");
       // values.put("password","1234");
        values.put("user","balabala");
        values.put("password","1234");
       // dataBaseHelper=new MyDataBaseHelper(this,"myfirstdatabase.db");

        db=SQLiteDatabase.openOrCreateDatabase("/mnt/sdcard/first.db",null);
        db.insert("regist",null,values);

        mLocationClient=new LocationClient(this.getApplicationContext());
        mLocationClient.registerLocationListener(new MyLocationListener());

        openMap=(ImageButton)findViewById(R.id.open_map);
        openMap.setOnClickListener(this);

        menu_deal_image=(ImageView)findViewById(R.id.menu_deal_image);
        menu_apollo_image=(ImageView)findViewById(R.id.menu_apollo_image);
        menu_poi_image=(ImageView)findViewById(R.id.menu_poi_image);
        menu_user_image=(ImageView)findViewById(R.id.menu_user_image);
        menu_more_image=(ImageView)findViewById(R.id.menu_more_image);
        menu_deal_text=(TextView)findViewById(R.id.menu_deal_text);
        menu_apollo_text=(TextView)findViewById(R.id.menu_apollo_text);
        menu_poi_text=(TextView)findViewById(R.id.menu_poi_text);
        menu_user_text=(TextView)findViewById(R.id.menu_user_text);
        menu_more_text=(TextView)findViewById(R.id.menu_more_text);



        content_fragment=(FrameLayout)findViewById(R.id.content_fragment);

        initLocation();
        content_fragment.addView(UILoading());
        setDefaultFragment();


    }

    Runnable mRunnable=new Runnable() {
        @Override
        public void run() {
            do {
                timeCount++;
                for(int i=0;i<2;i++){
                    // loading_img1.setImageResource(image[i]);
                    mHandler.sendEmptyMessageDelayed(i,200);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("the timeCount value is : "+timeCount);
                if(timeCount==15){
                    // mThread.
                    System.out.println("111111111111111111111111");
                    isStopThread=true;
                    // break;
                    mHandler.sendEmptyMessageDelayed(fragmentNum,100);
                }

            }while (timeCount<20);

        }
    };

    /*Thread mThread=new Thread(new Runnable() {
        @Override
        public void run() {
            do {
                timeCount++;
                for(int i=0;i<2;i++){
                    // loading_img1.setImageResource(image[i]);
                    mHandler.sendEmptyMessageDelayed(i,200);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("the timeCount value is : "+timeCount);
                if(timeCount==15){
                   // mThread.
                    System.out.println("111111111111111111111111");
                    isStopThread=true;
                   // break;
                    mHandler.sendEmptyMessageDelayed(2,100);
                }

            }while (timeCount<20);

        }
    });*/

    Handler mHandler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                loading_img1.setVisibility(View.VISIBLE);
                loading_img2.setVisibility(View.GONE);
            }
            if(msg.what==1){
                loading_img1.setVisibility(View.GONE);
                loading_img2.setVisibility(View.VISIBLE);
            }
            if(msg.what==2){
                FragmentManager fm=getFragmentManager();
                FragmentTransaction transaction=fm.beginTransaction();
                transaction.replace(R.id.content_fragment,tuanFragment);
                transaction.commit();
            }
            if(msg.what==3){
                FragmentManager fm=getFragmentManager();
                FragmentTransaction transaction=fm.beginTransaction();
                transaction.replace(R.id.content_fragment,shangMenFragment);
                transaction.commit();
            }
            if(msg.what==4){
               // setDefaultFragment();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction transaction=fm.beginTransaction();
                transaction.replace(R.id.content_fragment,businessFragment);
                transaction.commit();
            }
            if(msg.what==5){
               // setDefaultFragment();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction transaction=fm.beginTransaction();
                transaction.replace(R.id.content_fragment,mineFragment);
                transaction.commit();
            }
            if(msg.what==6){
               // setDefaultFragment();
                FragmentManager fm=getFragmentManager();
                FragmentTransaction transaction=fm.beginTransaction();
                transaction.replace(R.id.content_fragment,moreFragment);
                transaction.commit();
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

    }

    public View UILoading(){

        LayoutInflater inflater=LayoutInflater.from(this);
        View ui_loading= inflater.inflate(R.layout.ui_loading,null);
        loading_img1=(ImageView)ui_loading.findViewById(R.id.loading_img1);
        loading_img2=(ImageView)ui_loading.findViewById(R.id.loading_img2);

        return ui_loading;
    }

    public void setDefaultFragment(){
        fragmentNum=2;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        tuanFragment=new TuanFragment();
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_on);
        menu_deal_text.setTextColor(0xff009688);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_off);
        menu_apollo_text.setTextColor(0xff5a595b);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_off);
        menu_poi_text.setTextColor(0xff5a595b);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_off);
        menu_user_text.setTextColor(0xff5a595b);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_off);
        menu_more_text.setTextColor(0xff5a595b);


        //content_fragment.removeAllViews();



    }

    public void openTuanFragment(View view){
        fragmentNum=2;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        tuanFragment=new TuanFragment();
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_on);
        menu_deal_text.setTextColor(0xff009688);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_off);
        menu_apollo_text.setTextColor(0xff5a595b);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_off);
        menu_poi_text.setTextColor(0xff5a595b);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_off);
        menu_user_text.setTextColor(0xff5a595b);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_off);
        menu_more_text.setTextColor(0xff5a595b);


    }
    public void openShangMenFragment(View view){
        fragmentNum=3;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_off);
        menu_deal_text.setTextColor(0xff5a595b);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_on);
        menu_apollo_text.setTextColor(0xff009688);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_off);
        menu_poi_text.setTextColor(0xff5a595b);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_off);
        menu_user_text.setTextColor(0xff5a595b);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_off);
        menu_more_text.setTextColor(0xff5a595b);

        shangMenFragment=new ShangMenFragment();


    }
    public void openBusinessFragment(View view){
        fragmentNum=4;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        businessFragment=new BusinessFragment();
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_off);
        menu_deal_text.setTextColor(0xff5a595b);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_off);
        menu_apollo_text.setTextColor(0xff5a595b);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_on);
        menu_poi_text.setTextColor(0xff009688);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_off);
        menu_user_text.setTextColor(0xff5a595b);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_off);
        menu_more_text.setTextColor(0xff5a595b);




    }
    public void openMineFragment(View view){
        fragmentNum=5;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        mineFragment=new MineFragment();
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_off);
        menu_deal_text.setTextColor(0xff5a595b);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_off);
        menu_apollo_text.setTextColor(0xff5a595b);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_off);
        menu_poi_text.setTextColor(0xff5a595b);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_on);
        menu_user_text.setTextColor(0xff009688);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_off);
        menu_more_text.setTextColor(0xff5a595b);




    }
    public void openMoreFragment(View view){
        fragmentNum=6;
        timeCount=0;
        content_fragment.removeAllViews();
        content_fragment.addView(UILoading());
        moreFragment=new MoreFragment();
        new Thread(mRunnable).start();
        menu_deal_image.setBackgroundResource(R.mipmap.ic_menu_deal_off);
        menu_deal_text.setTextColor(0xff5a595b);
        menu_apollo_image.setBackgroundResource(R.mipmap.ic_menu_apollo_off);
        menu_apollo_text.setTextColor(0xff5a595b);
        menu_poi_image.setBackgroundResource(R.mipmap.ic_menu_poi_off);
        menu_poi_text.setTextColor(0xff5a595b);
        menu_user_image.setBackgroundResource(R.mipmap.ic_menu_user_off);
        menu_user_text.setTextColor(0xff5a595b);
        menu_more_image.setBackgroundResource(R.mipmap.ic_menu_more_on);
        menu_more_text.setTextColor(0xff009688);




    }

    public void getLocationCity(View view){
        dialog= MyProgressDialog.createLoadingDialog(this,"定位中...");
        dialog.show();
        mLocationClient.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_group_buy, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(tempMode);//设置定位模式
        option.setCoorType("gcj02");//返回的定位结果是百度经纬度，默认值gcj02
        int span=1000;

        option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
    }

    @Override
    public void onClick(View v) {

        if (v == openMap) {
            Intent intent=new Intent(this,MapViewActivity.class);
            startActivity(intent);
        }

    }

    class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation != null) {
                StringBuffer sb=new StringBuffer();
                String city=bdLocation.getCity();
                String district=bdLocation.getDistrict();
                if (city == null) {
                    cityTextView.setText("城市");
                    dialog.dismiss();
                    mLocationClient.stop();
                    return;
                }
                sb.append(city).append("-").append(district);
                cityTextView.setText(sb.toString());
                dialog.dismiss();
                mLocationClient.stop();
            }

        }
    }

}
